home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_kernel_source / FS / EXEC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-17  |  20.2 KB  |  859 lines

  1. /*
  2.  *  linux/fs/exec.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  */
  6.  
  7. /*
  8.  * #!-checking implemented by tytso.
  9.  */
  10. /*
  11.  * Demand-loading implemented 01.12.91 - no need to read anything but
  12.  * the header into memory. The inode of the executable is put into
  13.  * "current->executable", and page faults do the actual loading. Clean.
  14.  *
  15.  * Once more I can proudly say that linux stood up to being changed: it
  16.  * was less than 2 hours work to get demand-loading completely implemented.
  17.  *
  18.  * Demand loading changed July 1993 by Eric Youngdale.   Use mmap instead,
  19.  * current->executable is only used by the procfs.  This allows a dispatch
  20.  * table to check for several different types  of binary formats.  We keep
  21.  * trying until we recognize the file or we run out of supported binary
  22.  * formats. 
  23.  */
  24.  
  25. #include <linux/config.h>
  26. #include <linux/slab.h>
  27. #include <linux/file.h>
  28. #include <linux/mman.h>
  29. #include <linux/a.out.h>
  30. #include <linux/stat.h>
  31. #include <linux/fcntl.h>
  32. #include <linux/user.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/init.h>
  35.  
  36. #include <asm/uaccess.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/mmu_context.h>
  39.  
  40. #ifdef CONFIG_KMOD
  41. #include <linux/kmod.h>
  42. #endif
  43.  
  44. /*
  45.  * Here are the actual binaries that will be accepted:
  46.  * add more with "register_binfmt()" if using modules...
  47.  *
  48.  * These are defined again for the 'real' modules if you are using a
  49.  * module definition for these routines.
  50.  */
  51.  
  52. static struct linux_binfmt *formats = (struct linux_binfmt *) NULL;
  53.  
  54. void __init binfmt_setup(void)
  55. {
  56. #ifdef CONFIG_BINFMT_MISC
  57.     init_misc_binfmt();
  58. #endif
  59.  
  60. #ifdef CONFIG_BINFMT_ELF
  61.     init_elf_binfmt();
  62. #endif
  63.  
  64. #ifdef CONFIG_BINFMT_ELF32
  65.     init_elf32_binfmt();
  66. #endif
  67.  
  68. #ifdef CONFIG_BINFMT_AOUT
  69.     init_aout_binfmt();
  70. #endif
  71.  
  72. #ifdef CONFIG_BINFMT_AOUT32
  73.     init_aout32_binfmt();
  74. #endif
  75.  
  76. #ifdef CONFIG_BINFMT_JAVA
  77.     init_java_binfmt();
  78. #endif
  79.  
  80. #ifdef CONFIG_BINFMT_EM86
  81.     init_em86_binfmt();
  82. #endif
  83.  
  84.     /* This cannot be configured out of the kernel */
  85.     init_script_binfmt();
  86. }
  87.  
  88. int register_binfmt(struct linux_binfmt * fmt)
  89. {
  90.     struct linux_binfmt ** tmp = &formats;
  91.  
  92.     if (!fmt)
  93.         return -EINVAL;
  94.     if (fmt->next)
  95.         return -EBUSY;
  96.     while (*tmp) {
  97.         if (fmt == *tmp)
  98.             return -EBUSY;
  99.         tmp = &(*tmp)->next;
  100.     }
  101.     fmt->next = formats;
  102.     formats = fmt;
  103.     return 0;    
  104. }
  105.  
  106. #ifdef CONFIG_MODULES
  107. int unregister_binfmt(struct linux_binfmt * fmt)
  108. {
  109.     struct linux_binfmt ** tmp = &formats;
  110.  
  111.     while (*tmp) {
  112.         if (fmt == *tmp) {
  113.             *tmp = fmt->next;
  114.             return 0;
  115.         }
  116.         tmp = &(*tmp)->next;
  117.     }
  118.     return -EINVAL;
  119. }
  120. #endif    /* CONFIG_MODULES */
  121.  
  122. /* N.B. Error returns must be < 0 */
  123. int open_dentry(struct dentry * dentry, int mode)
  124. {
  125.     struct inode * inode = dentry->d_inode;
  126.     struct file * f;
  127.     int fd, error;
  128.  
  129.     error = -EINVAL;
  130.     if (!inode->i_op || !inode->i_op->default_file_ops)
  131.         goto out;
  132.     fd = get_unused_fd();
  133.     if (fd >= 0) {
  134.         error = -ENFILE;
  135.         f = get_empty_filp();
  136.         if (!f)
  137.             goto out_fd;
  138.         f->f_flags = mode;
  139.         f->f_mode = (mode+1) & O_ACCMODE;
  140.         f->f_dentry = dentry;
  141.         f->f_pos = 0;
  142.         f->f_reada = 0;
  143.         f->f_op = inode->i_op->default_file_ops;
  144.         if (f->f_op->open) {
  145.             error = f->f_op->open(inode,f);
  146.             if (error)
  147.                 goto out_filp;
  148.         }
  149.         fd_install(fd, f);
  150.         dget(dentry);
  151.     }
  152.     return fd;
  153.  
  154. out_filp:
  155.     if (error > 0)
  156.         error = -EIO;
  157.     put_filp(f);
  158. out_fd:
  159.     put_unused_fd(fd);
  160. out:
  161.     return error;
  162. }
  163.  
  164. /*
  165.  * Note that a shared library must be both readable and executable due to
  166.  * security reasons.
  167.  *
  168.  * Also note that we take the address to load from from the file itself.
  169.  */
  170. asmlinkage int sys_uselib(const char * library)
  171. {
  172.     int fd, retval;
  173.     struct file * file;
  174.     struct linux_binfmt * fmt;
  175.  
  176.     lock_kernel();
  177.     fd = sys_open(library, 0, 0);
  178.     retval = fd;
  179.     if (fd < 0)
  180.         goto out;
  181.     file = fget(fd);
  182.     retval = -ENOEXEC;
  183.     if (file && file->f_dentry && file->f_op && file->f_op->read) {
  184.         for (fmt = formats ; fmt ; fmt = fmt->next) {
  185.             int (*fn)(int) = fmt->load_shlib;
  186.             if (!fn)
  187.                 continue;
  188.             /* N.B. Should use file instead of fd */
  189.             retval = fn(fd);
  190.             if (retval != -ENOEXEC)
  191.                 break;
  192.         }
  193.     }
  194.     fput(file);
  195.     sys_close(fd);
  196. out:
  197.     unlock_kernel();
  198.       return retval;
  199. }
  200.  
  201. /*
  202.  * count() counts the number of arguments/envelopes
  203.  */
  204. static int count(char ** argv)
  205. {
  206.     int i = 0;
  207.  
  208.     if (argv != NULL) {
  209.         for (;;) {
  210.             char * p;
  211.             int error;
  212.  
  213.             error = get_user(p,argv);
  214.             if (error)
  215.                 return error;
  216.             if (!p)
  217.                 break;
  218.             argv++;
  219.             i++;
  220.         }
  221.     }
  222.     return i;
  223. }
  224.  
  225. /*
  226.  * 'copy_string()' copies argument/envelope strings from user
  227.  * memory to free pages in kernel mem. These are in a format ready
  228.  * to be put directly into the top of new user memory.
  229.  *
  230.  * Modified by TYT, 11/24/91 to add the from_kmem argument, which specifies
  231.  * whether the string and the string array are from user or kernel segments:
  232.  * 
  233.  * from_kmem     argv *        argv **
  234.  *    0          user space    user space
  235.  *    1          kernel space  user space
  236.  *    2          kernel space  kernel space
  237.  * 
  238.  * We do this by playing games with the fs segment register.  Since it
  239.  * is expensive to load a segment register, we try to avoid calling
  240.  * set_fs() unless we absolutely have to.
  241.  */
  242. unsigned long copy_strings(int argc,char ** argv,unsigned long *page,
  243.         unsigned long p, int from_kmem)
  244. {
  245.     char *str;
  246.     mm_segment_t old_fs;
  247.  
  248.     if (!p)
  249.         return 0;    /* bullet-proofing */
  250.     old_fs = get_fs();
  251.     if (from_kmem==2)
  252.         set_fs(KERNEL_DS);
  253.     while (argc-- > 0) {
  254.         int len;
  255.         unsigned long pos;
  256.  
  257.         if (from_kmem == 1)
  258.             set_fs(KERNEL_DS);
  259.         get_user(str, argv+argc);
  260.         if (!str)
  261.             panic("VFS: argc is wrong");
  262.         if (from_kmem == 1)
  263.             set_fs(old_fs);
  264.         len = strlen_user(str);    /* includes the '\0' */
  265.         if (p < len) {    /* this shouldn't happen - 128kB */
  266.             set_fs(old_fs);
  267.             return 0;
  268.         }
  269.         p -= len;
  270.         pos = p;
  271.         while (len) {
  272.             char *pag;
  273.             int offset, bytes_to_copy;
  274.  
  275.             offset = pos % PAGE_SIZE;
  276.             if (!(pag = (char *) page[pos/PAGE_SIZE]) &&
  277.                 !(pag = (char *) page[pos/PAGE_SIZE] =
  278.                   (unsigned long *) get_free_page(GFP_USER))) {
  279.                 if (from_kmem==2)
  280.                     set_fs(old_fs);
  281.                 return 0;
  282.             }
  283.             bytes_to_copy = PAGE_SIZE - offset;
  284.             if (bytes_to_copy > len)
  285.                 bytes_to_copy = len;
  286.             copy_from_user(pag + offset, str, bytes_to_copy);
  287.             pos += bytes_to_copy;
  288.             str += bytes_to_copy;
  289.             len -= bytes_to_copy;
  290.         }
  291.     }
  292.     if (from_kmem==2)
  293.         set_fs(old_fs);
  294.     return p;
  295. }
  296.  
  297. unsigned long setup_arg_pages(unsigned long p, struct linux_binprm * bprm)
  298. {
  299.     unsigned long stack_base;
  300.     struct vm_area_struct *mpnt;
  301.     int i;
  302.  
  303.     stack_base = STACK_TOP - MAX_ARG_PAGES*PAGE_SIZE;
  304.  
  305.     p += stack_base;
  306.     if (bprm->loader)
  307.         bprm->loader += stack_base;
  308.     bprm->exec += stack_base;
  309.  
  310.     mpnt = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  311.     if (mpnt) {
  312.         mpnt->vm_mm = current->mm;
  313.         mpnt->vm_start = PAGE_MASK & (unsigned long) p;
  314.         mpnt->vm_end = STACK_TOP;
  315.         mpnt->vm_page_prot = PAGE_COPY;
  316.         mpnt->vm_flags = VM_STACK_FLAGS;
  317.         mpnt->vm_ops = NULL;
  318.         mpnt->vm_offset = 0;
  319.         mpnt->vm_file = NULL;
  320.         mpnt->vm_pte = 0;
  321.         insert_vm_struct(current->mm, mpnt);
  322.         current->mm->total_vm = (mpnt->vm_end - mpnt->vm_start) >> PAGE_SHIFT;
  323.     }
  324.  
  325.     for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
  326.         if (bprm->page[i]) {
  327.             current->mm->rss++;
  328.             put_dirty_page(current,bprm->page[i],stack_base);
  329.         }
  330.         stack_base += PAGE_SIZE;
  331.     }
  332.     return p;
  333. }
  334.  
  335. /*
  336.  * Read in the complete executable. This is used for "-N" files
  337.  * that aren't on a block boundary, and for files on filesystems
  338.  * without bmap support.
  339.  */
  340. int read_exec(struct dentry *dentry, unsigned long offset,
  341.     char * addr, unsigned long count, int to_kmem)
  342. {
  343.     struct file file;
  344.     struct inode * inode = dentry->d_inode;
  345.     int result = -ENOEXEC;
  346.  
  347.     if (!inode->i_op || !inode->i_op->default_file_ops)
  348.         goto end_readexec;
  349.     if (init_private_file(&file, dentry, 1))
  350.         goto end_readexec;
  351.     if (!file.f_op->read)
  352.         goto close_readexec;
  353.     if (file.f_op->llseek) {
  354.         if (file.f_op->llseek(&file,offset,0) != offset)
  355.              goto close_readexec;
  356.     } else
  357.         file.f_pos = offset;
  358.     if (to_kmem) {
  359.         mm_segment_t old_fs = get_fs();
  360.         set_fs(get_ds());
  361.         result = file.f_op->read(&file, addr, count, &file.f_pos);
  362.         set_fs(old_fs);
  363.     } else {
  364.         result = verify_area(VERIFY_WRITE, addr, count);
  365.         if (result)
  366.             goto close_readexec;
  367.         result = file.f_op->read(&file, addr, count, &file.f_pos);
  368.     }
  369. close_readexec:
  370.     if (file.f_op->release)
  371.         file.f_op->release(inode,&file);
  372. end_readexec:
  373.     return result;
  374. }
  375.  
  376. static int exec_mmap(void)
  377. {
  378.     struct mm_struct * mm, * old_mm;
  379.     int retval, nr;
  380.  
  381.     if (atomic_read(¤t->mm->count) == 1) {
  382.         flush_cache_mm(current->mm);
  383.         mm_release();
  384.         release_segments(current->mm);
  385.         exit_mmap(current->mm);
  386.         flush_tlb_mm(current->mm);
  387.         return 0;
  388.     }
  389.  
  390.     retval = -ENOMEM;
  391.     mm = mm_alloc();
  392.     if (!mm)
  393.         goto fail_nomem;
  394.  
  395.     mm->cpu_vm_mask = (1UL << smp_processor_id());
  396.     mm->total_vm = 0;
  397.     mm->rss = 0;
  398.     /*
  399.      * Make sure we have a private ldt if needed ...
  400.      */
  401.     nr = current->tarray_ptr - &task[0]; 
  402.     copy_segments(nr, current, mm);
  403.  
  404.     old_mm = current->mm;
  405.     current->mm = mm;
  406.     retval = new_page_tables(current);
  407.     if (retval)
  408.         goto fail_restore;
  409.     activate_context(current);
  410.     up(&mm->mmap_sem);
  411.     mm_release();
  412.     mmput(old_mm);
  413.     return 0;
  414.  
  415.     /*
  416.      * Failure ... restore the prior mm_struct.
  417.      */
  418. fail_restore:
  419.     /* The pgd belongs to the parent ... don't free it! */
  420.     mm->pgd = NULL;
  421.     current->mm = old_mm;
  422.     /* restore the ldt for this task */
  423.     copy_segments(nr, current, NULL);
  424.     mmput(mm);
  425.  
  426. fail_nomem:
  427.     return retval;
  428. }
  429.  
  430. /*
  431.  * This function makes sure the current process has its own signal table,
  432.  * so that flush_signal_handlers can later reset the handlers without
  433.  * disturbing other processes.  (Other processes might share the signal
  434.  * table via the CLONE_SIGHAND option to clone().)
  435.  */
  436.  
  437. static inline int make_private_signals(void)
  438. {
  439.     struct signal_struct * newsig;
  440.  
  441.     if (atomic_read(¤t->sig->count) <= 1)
  442.         return 0;
  443.     newsig = kmalloc(sizeof(*newsig), GFP_KERNEL);
  444.     if (newsig == NULL)
  445.         return -ENOMEM;
  446.     spin_lock_init(&newsig->siglock);
  447.     atomic_set(&newsig->count, 1);
  448.     memcpy(newsig->action, current->sig->action, sizeof(newsig->action));
  449.     current->sig = newsig;
  450.     return 0;
  451. }
  452.     
  453. /*
  454.  * If make_private_signals() made a copy of the signal table, decrement the
  455.  * refcount of the original table, and free it if necessary.
  456.  * We don't do that in make_private_signals() so that we can back off
  457.  * in flush_old_exec() if an error occurs after calling make_private_signals().
  458.  */
  459.  
  460. static inline void release_old_signals(struct signal_struct * oldsig)
  461. {
  462.     if (current->sig == oldsig)
  463.         return;
  464.     if (atomic_dec_and_test(&oldsig->count))
  465.         kfree(oldsig);
  466. }
  467.  
  468. /*
  469.  * These functions flushes out all traces of the currently running executable
  470.  * so that a new one can be started
  471.  */
  472.  
  473. static inline void flush_old_files(struct files_struct * files)
  474. {
  475.     unsigned long j;
  476.  
  477.     j = 0;
  478.     for (;;) {
  479.         unsigned long set, i;
  480.  
  481.         i = j * __NFDBITS;
  482.         if (i >= files->max_fds)
  483.             break;
  484.         set = files->close_on_exec.fds_bits[j];
  485.         files->close_on_exec.fds_bits[j] = 0;
  486.         j++;
  487.         for ( ; set ; i++,set >>= 1) {
  488.             if (set & 1)
  489.                 sys_close(i);
  490.         }
  491.     }
  492. }
  493.  
  494. int flush_old_exec(struct linux_binprm * bprm)
  495. {
  496.     char * name;
  497.     int i, ch, retval;
  498.     struct signal_struct * oldsig;
  499.  
  500.     /*
  501.      * Make sure we have a private signal table
  502.      */
  503.     oldsig = current->sig;
  504.     retval = make_private_signals();
  505.     if (retval) goto flush_failed;
  506.  
  507.     /* 
  508.      * Release all of the old mmap stuff
  509.      */
  510.     retval = exec_mmap();
  511.     if (retval) goto mmap_failed;
  512.  
  513.     /* This is the point of no return */
  514.     release_old_signals(oldsig);
  515.  
  516.     if (current->euid == current->uid && current->egid == current->gid)
  517.         current->dumpable = 1;
  518.     name = bprm->filename;
  519.     for (i=0; (ch = *(name++)) != '\0';) {
  520.         if (ch == '/')
  521.             i = 0;
  522.         else
  523.             if (i < 15)
  524.                 current->comm[i++] = ch;
  525.     }
  526.     current->comm[i] = '\0';
  527.  
  528.     flush_thread();
  529.  
  530.     if (bprm->e_uid != current->euid || bprm->e_gid != current->egid || 
  531.         permission(bprm->dentry->d_inode,MAY_READ))
  532.         current->dumpable = 0;
  533.  
  534.     flush_signal_handlers(current);
  535.     flush_old_files(current->files);
  536.  
  537.     return 0;
  538.  
  539. mmap_failed:
  540.     if (current->sig != oldsig)
  541.         kfree(current->sig);
  542. flush_failed:
  543.     current->sig = oldsig;
  544.     return retval;
  545. }
  546.  
  547. /*
  548.  * We mustn't allow tracing of suid binaries, unless
  549.  * the tracer has the capability to trace anything..
  550.  */
  551. static inline int must_not_trace_exec(struct task_struct * p)
  552. {
  553.     return (p->flags & PF_PTRACED) && !cap_raised(p->p_pptr->cap_effective, CAP_SYS_PTRACE);
  554. }
  555.  
  556. /* 
  557.  * Fill the binprm structure from the inode. 
  558.  * Check permissions, then read the first 512 bytes
  559.  */
  560. int prepare_binprm(struct linux_binprm *bprm)
  561. {
  562.     int mode;
  563.     int retval,id_change,cap_raised;
  564.     struct inode * inode = bprm->dentry->d_inode;
  565.  
  566.     mode = inode->i_mode;
  567.     if (!S_ISREG(mode))            /* must be regular file */
  568.         return -EACCES;
  569.     if (!(mode & 0111))            /* with at least _one_ execute bit set */
  570.         return -EACCES;
  571.     if (IS_NOEXEC(inode))            /* FS mustn't be mounted noexec */
  572.         return -EACCES;
  573.     if (!inode->i_sb)
  574.         return -EACCES;
  575.     if ((retval = permission(inode, MAY_EXEC)) != 0)
  576.         return retval;
  577.     /* better not execute files which are being written to */
  578.     if (inode->i_writecount > 0)
  579.         return -ETXTBSY;
  580.  
  581.     bprm->e_uid = current->euid;
  582.     bprm->e_gid = current->egid;
  583.     id_change = cap_raised = 0;
  584.  
  585.     /* Set-uid? */
  586.     if (mode & S_ISUID) {
  587.         bprm->e_uid = inode->i_uid;
  588.         if (bprm->e_uid != current->euid)
  589.             id_change = 1;
  590.     }
  591.  
  592.     /* Set-gid? */
  593.     /*
  594.      * If setgid is set but no group execute bit then this
  595.      * is a candidate for mandatory locking, not a setgid
  596.      * executable.
  597.      */
  598.     if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
  599.         bprm->e_gid = inode->i_gid;
  600.         if (!in_group_p(bprm->e_gid))
  601.             id_change = 1;
  602.     }
  603.  
  604.     /* We don't have VFS support for capabilities yet */
  605.     cap_clear(bprm->cap_inheritable);
  606.     cap_clear(bprm->cap_permitted);
  607.     cap_clear(bprm->cap_effective);
  608.  
  609.     /*  To support inheritance of root-permissions and suid-root
  610.          *  executables under compatibility mode, we raise the
  611.          *  effective and inherited bitmasks of the executable file
  612.          *  (translation: we set the executable "capability dumb" and
  613.          *  set the allowed set to maximum). We don't set any forced
  614.          *  bits.
  615.          *
  616.          *  If only the real uid is 0, we only raise the inheritable
  617.          *  bitmask of the executable file (translation: we set the
  618.          *  allowed set to maximum and the application to "capability
  619.          *  smart"). 
  620.          */
  621.  
  622.     if (!issecure(SECURE_NOROOT)) {
  623.         if (bprm->e_uid == 0 || current->uid == 0)
  624.             cap_set_full(bprm->cap_inheritable);
  625.         if (bprm->e_uid == 0) 
  626.             cap_set_full(bprm->cap_effective);
  627.     }
  628.  
  629.         /* Only if pP' is _not_ a subset of pP, do we consider there
  630.          * has been a capability related "change of capability".  In
  631.          * such cases, we need to check that the elevation of
  632.          * privilege does not go against other system constraints.
  633.          * The new Permitted set is defined below -- see (***). */
  634.     {
  635.         kernel_cap_t working =
  636.             cap_combine(bprm->cap_permitted,
  637.                     cap_intersect(bprm->cap_inheritable,
  638.                           current->cap_inheritable));
  639.         if (!cap_issubset(working, current->cap_permitted)) {
  640.             cap_raised = 1;
  641.         }
  642.     }
  643.  
  644.     if (id_change || cap_raised) {
  645.         /* We can't suid-execute if we're sharing parts of the executable */
  646.         /* or if we're being traced (or if suid execs are not allowed)    */
  647.         /* (current->mm->count > 1 is ok, as we'll get a new mm anyway)   */
  648.         if (IS_NOSUID(inode)
  649.             || must_not_trace_exec(current)
  650.             || (atomic_read(¤t->fs->count) > 1)
  651.             || (atomic_read(¤t->sig->count) > 1)
  652.             || (atomic_read(¤t->files->count) > 1)) {
  653.              if (id_change && !capable(CAP_SETUID))
  654.                  return -EPERM;
  655.              if (cap_raised && !capable(CAP_SETPCAP))
  656.                   return -EPERM;
  657.         }
  658.     }
  659.  
  660.     memset(bprm->buf,0,sizeof(bprm->buf));
  661.     return read_exec(bprm->dentry,0,bprm->buf,128,1);
  662. }
  663.  
  664. /*
  665.  * This function is used to produce the new IDs and capabilities
  666.  * from the old ones and the file's capabilities.
  667.  *
  668.  * The formula used for evolving capabilities is:
  669.  *
  670.  *       pI' = pI
  671.  * (***) pP' = fP | (fI & pI)
  672.  *       pE' = pP' & fE          [NB. fE is 0 or ~0]
  673.  *
  674.  * I=Inheritable, P=Permitted, E=Effective // p=process, f=file
  675.  * ' indicates post-exec().
  676.  */
  677.  
  678. void compute_creds(struct linux_binprm *bprm) 
  679. {
  680.     int new_permitted = cap_t(bprm->cap_permitted) |
  681.         (cap_t(bprm->cap_inheritable) & 
  682.          cap_t(current->cap_inheritable));
  683.  
  684.     /* For init, we want to retain the capabilities set
  685.          * in the init_task struct. Thus we skip the usual
  686.          * capability rules */
  687.     if (current->pid != 1) {
  688.         cap_t(current->cap_permitted) = new_permitted;
  689.         cap_t(current->cap_effective) = new_permitted & 
  690.                         cap_t(bprm->cap_effective);
  691.     }
  692.     
  693.         /* AUD: Audit candidate if current->cap_effective is set */
  694.  
  695.         current->suid = current->euid = current->fsuid = bprm->e_uid;
  696.         current->sgid = current->egid = current->fsgid = bprm->e_gid;
  697.         if (current->euid != current->uid || current->egid != current->gid ||
  698.         !cap_issubset(new_permitted, current->cap_permitted))
  699.                 current->dumpable = 0;
  700. }
  701.  
  702.  
  703. void remove_arg_zero(struct linux_binprm *bprm)
  704. {
  705.     if (bprm->argc) {
  706.         unsigned long offset;
  707.         char * page;
  708.         offset = bprm->p % PAGE_SIZE;
  709.         page = (char*)bprm->page[bprm->p/PAGE_SIZE];
  710.         while(bprm->p++,*(page+offset++))
  711.             if(offset==PAGE_SIZE){
  712.                 offset=0;
  713.                 page = (char*)bprm->page[bprm->p/PAGE_SIZE];
  714.             }
  715.         bprm->argc--;
  716.     }
  717. }
  718.  
  719. /*
  720.  * cycle the list of binary formats handler, until one recognizes the image
  721.  */
  722. int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
  723. {
  724.     int try,retval=0;
  725.     struct linux_binfmt *fmt;
  726. #ifdef __alpha__
  727.     /* handle /sbin/loader.. */
  728.     {
  729.         struct exec * eh = (struct exec *) bprm->buf;
  730.         struct linux_binprm bprm_loader;
  731.  
  732.         if (!bprm->loader && eh->fh.f_magic == 0x183 &&
  733.         (eh->fh.f_flags & 0x3000) == 0x3000)
  734.         {
  735.         int i;
  736.         char * dynloader[] = { "/sbin/loader" };
  737.         struct dentry * dentry;
  738.  
  739.         dput(bprm->dentry);
  740.         bprm->dentry = NULL;
  741.  
  742.             bprm_loader.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
  743.             for (i=0 ; i<MAX_ARG_PAGES ; i++)       /* clear page-table */
  744.                     bprm_loader.page[i] = 0;
  745.  
  746.         dentry = open_namei(dynloader[0], 0, 0);
  747.         retval = PTR_ERR(dentry);
  748.         if (IS_ERR(dentry))
  749.             return retval;
  750.         bprm->dentry = dentry;
  751.         bprm->loader = bprm_loader.p;
  752.         retval = prepare_binprm(bprm);
  753.         if (retval<0)
  754.             return retval;
  755.         /* should call search_binary_handler recursively here,
  756.            but it does not matter */
  757.         }
  758.     }
  759. #endif
  760.     for (try=0; try<2; try++) {
  761.         for (fmt = formats ; fmt ; fmt = fmt->next) {
  762.             int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
  763.             if (!fn)
  764.                 continue;
  765.             retval = fn(bprm, regs);
  766.             if (retval >= 0) {
  767.                 if (bprm->dentry)
  768.                     dput(bprm->dentry);
  769.                 bprm->dentry = NULL;
  770.                 current->did_exec = 1;
  771.                 return retval;
  772.             }
  773.             if (retval != -ENOEXEC)
  774.                 break;
  775.             if (!bprm->dentry) /* We don't have the dentry anymore */
  776.                 return retval;
  777.         }
  778.         if (retval != -ENOEXEC) {
  779.             break;
  780. #ifdef CONFIG_KMOD
  781.         }else{
  782. #define printable(c) (((c)=='\t') || ((c)=='\n') || (0x20<=(c) && (c)<=0x7e))
  783.             char modname[20];
  784.             if (printable(bprm->buf[0]) &&
  785.                 printable(bprm->buf[1]) &&
  786.                 printable(bprm->buf[2]) &&
  787.                 printable(bprm->buf[3]))
  788.                 break; /* -ENOEXEC */
  789.             sprintf(modname, "binfmt-%04x", *(unsigned short *)(&bprm->buf[2]));
  790.             request_module(modname);
  791. #endif
  792.         }
  793.     }
  794.     return retval;
  795. }
  796.  
  797.  
  798. /*
  799.  * sys_execve() executes a new program.
  800.  */
  801. int do_execve(char * filename, char ** argv, char ** envp, struct pt_regs * regs)
  802. {
  803.     struct linux_binprm bprm;
  804.     struct dentry * dentry;
  805.     int retval;
  806.     int i;
  807.  
  808.     bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
  809.     for (i=0 ; i<MAX_ARG_PAGES ; i++)    /* clear page-table */
  810.         bprm.page[i] = 0;
  811.  
  812.     dentry = open_namei(filename, 0, 0);
  813.     retval = PTR_ERR(dentry);
  814.     if (IS_ERR(dentry))
  815.         return retval;
  816.  
  817.     bprm.dentry = dentry;
  818.     bprm.filename = filename;
  819.     bprm.sh_bang = 0;
  820.     bprm.java = 0;
  821.     bprm.loader = 0;
  822.     bprm.exec = 0;
  823.     if ((bprm.argc = count(argv)) < 0) {
  824.         dput(dentry);
  825.         return bprm.argc;
  826.     }
  827.  
  828.     if ((bprm.envc = count(envp)) < 0) {
  829.         dput(dentry);
  830.         return bprm.envc;
  831.     }
  832.  
  833.     retval = prepare_binprm(&bprm);
  834.     
  835.     if (retval >= 0) {
  836.         bprm.p = copy_strings(1, &bprm.filename, bprm.page, bprm.p, 2);
  837.         bprm.exec = bprm.p;
  838.         bprm.p = copy_strings(bprm.envc,envp,bprm.page,bprm.p,0);
  839.         bprm.p = copy_strings(bprm.argc,argv,bprm.page,bprm.p,0);
  840.         if (!bprm.p)
  841.             retval = -E2BIG;
  842.     }
  843.  
  844.     if (retval >= 0)
  845.         retval = search_binary_handler(&bprm,regs);
  846.     if (retval >= 0)
  847.         /* execve success */
  848.         return retval;
  849.  
  850.     /* Something went wrong, return the inode and free the argument pages*/
  851.     if (bprm.dentry)
  852.         dput(bprm.dentry);
  853.  
  854.     for (i=0 ; i<MAX_ARG_PAGES ; i++)
  855.         free_page(bprm.page[i]);
  856.  
  857.     return retval;
  858. }
  859.